package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadMetroStationGetwayBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadMetroStation;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadMetroStationGetway;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.model.selectmappoint.SelectedPoint;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地铁出入口
 */
public class RoadMetroStationGetwayFragment extends Fragment {
    private FragmentRoadMetroStationGetwayBinding binding;

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadMetroStationGetway roadMetroStationGetway;

    // 站台类型 数据字典
    private List<String> platformTypeDict = new ArrayList<>();
    // 用地类型 数据字典
    private List<String> landUseTypeDict = new ArrayList<>();
    // 坐标
    private String selectedPoint;

    public RoadMetroStationGetwayFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_metro_station_getway, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        platformTypeDict = Enums.MetroGateWayPlatformTypeEnum.getEnumValueList();
        landUseTypeDict = Enums.MetroGateWaylandUseTypeEnum.getEnumValueList();
        roadMetroStationGetway = roadFacilitiesTaskDTO.getRoadMetroStationGetway() == null ? new RoadMetroStationGetway() : roadFacilitiesTaskDTO.getRoadMetroStationGetway();
    }

    private void initView() {
        // 获取坐标
        SelectedPoint selectedPoint = JSONObject.parseObject(roadMetroStationGetway.getGatewayCoordinate(), SelectedPoint.class);
        selectedPoint = selectedPoint == null ? new SelectedPoint() : selectedPoint;

        binding.metroName.setValue(roadMetroStationGetway.getMetroName());
        binding.platformType.setValue(Enums.MetroGateWayPlatformTypeEnum.getValueByKey(roadMetroStationGetway.getPlatformType()));
        binding.gatewayCoordinate.setValue(selectedPoint.getxCoordinate(), selectedPoint.getyCoordinate());
        binding.gatewayWidth.setValue(roadMetroStationGetway.getGatewayWidth());
        binding.landUseType.setValue(Enums.MetroGateWaylandUseTypeEnum.getValueByKey(roadMetroStationGetway.getLandUseType()));
    }

    public RoadMetroStationGetway getData() {
        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadMetroStationGetway.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        roadMetroStationGetway.setMetroName(binding.metroName.getStringValue());
        roadMetroStationGetway.setPlatformType(Enums.MetroGateWayPlatformTypeEnum.getKeyByValue(binding.platformType.getStringValue()));
        roadMetroStationGetway.setGatewayCoordinate(selectedPoint);
        roadMetroStationGetway.setGatewayWidth(binding.gatewayWidth.getBigDecimalValue());
        roadMetroStationGetway.setLandUseType(Enums.MetroGateWaylandUseTypeEnum.getKeyByValue(binding.landUseType.getStringValue()));

        return roadMetroStationGetway;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectedPoint centerPoint = null;
        //选择了坐标
        if (resultCode == Constants.RESULT_CODE.SELECTED_POINT) {
            if (data != null) {
                Bundle presentValue = data.getExtras();
                centerPoint = (SelectedPoint) presentValue.getSerializable(Constants.EXTRA_KEY.MAP_CENTER_POINT);
            }
        }
        if (centerPoint == null) {
            return;
        }
        selectedPoint = JSONObject.toJSONString(centerPoint);
    }

    @OnClick({R.id.platformType, R.id.landUseType})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        switch (view.getId()) {
            case R.id.platformType:
                RollUtil.alertBottomWheelOption(getContext(), platformTypeDict, binding.platformType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.platformType.setValue(platformTypeDict.get(index));
                    }
                });
                break;
            case R.id.landUseType:
                RollUtil.alertBottomWheelOption(getContext(), landUseTypeDict, binding.landUseType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.landUseType.setValue(landUseTypeDict.get(index));
                    }
                });
        }
    }

}